package com.xw.tools.view;

import com.xw.tools.util.MD5Utils;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Md5Win extends AbstractWin {

    private Button button = new Button("MD5");

    private TextArea inputTextArea;

    private TextArea outputTextArea;

    private ChoiceBox cb;

    private void start() {
        Stage stage = new Stage();
        stage.setTitle("md5");
        //stage.initModality(Modality.WINDOW_MODAL);
        //stage.initOwner(primaryStage);
        Scene scene = new Scene(new VBox(), 800, 495);
        initView(scene);
        stage.setScene(scene);
        stage.show();
    }

    private void initView(Scene scene) {
        inputTextArea = new TextArea();
        outputTextArea = new TextArea();
        inputTextArea.setFont(Font.font(16));
        outputTextArea.setFont(Font.font(16));
        outputTextArea.setEditable(false);
        button.setOnAction((ActionEvent t) -> {
            String text = inputTextArea.getText();
            if (text.isEmpty()) {
                return;
            }
            String md5Encode = MD5Utils.MD5Encode(text, (String) cb.getValue());
            outputTextArea.setText(md5Encode);
        });
        VBox box = (VBox) scene.getRoot();
        box.getChildren().add(inputTextArea);
        HBox hBox = new HBox(4);
        hBox.getChildren().add(button);
//        Label label = new Label("charset:");
//        hBox.getChildren().add(label);
        cb = new ChoiceBox(FXCollections.observableArrayList(
                "UTF-8", "GBK")
        );
        cb.setValue("UTF-8");
        hBox.getChildren().add(cb);
        box.getChildren().add(hBox);
        box.getChildren().add(outputTextArea);
        VBox.setVgrow(inputTextArea,  Priority.ALWAYS);
        VBox.setVgrow(outputTextArea,  Priority.ALWAYS);
    }

    @Override
    public void show() {
        start();
    }
}
